using System;
using M13.Base;
using M13.Game.Item;
using UnityEngine;
using QFramework;
using M13.GameSystem;
using M13.Utility;

namespace M13.Game.Entity.Player
{
    public class PlayerEntity : Entity
    {
        [Space(10)]
        [SerializeField] private GameObject handHeldR;
        [SerializeField] private GameObject handHeldL;

        
        private PlayerInventory _inventory;
        private PlayerStateMachine _stateMachine;
        private IInputCtrlSystem _inputCtrlSystem;

        
        private void Start()
        {
            _inventory = GetComponent<PlayerInventory>();
            _inputCtrlSystem = this.GetSystem<IInputCtrlSystem>();
            _stateMachine = GetComponent<PlayerStateMachine>();
            
            this.RegisterEvent<Evecute0Button>((e) =>
            {
                if (e.execute0Input == 1f)
                    InstructExecute();
            });
            this.RegisterEvent<Evecute1Button>((e) =>
            {
                if (e.execute1Input == 1f)
                    InstructExecute();
            });
            
            
        }
        
        private void Update()
        {
            this.GetModel<IPlayerModal>().PlayerPos.Value = transform.position;
            
            
        }
        /*
         * 可能包含的所有操作：
         * 手持武器时左键点击实体/可被攻击的生物：攻击生物
         * 手持武器时右键点击实体/可被攻击的生物：与生物交互
         * 手持武器时左键点击功能性方块：进入攻击状态，即使没有生物
         * 手持武器时右键点击功能性方块：与功能方块交互：打开HUD或其他操作
         * 手持食物时左键点击任何方块：无反应
         * 手持食物时右键非实体/非功能方块：吃
         * 手持食物时右键实体：进行与食物有关的操作
         * 手持食物时右键功能方块，与功能方块交互：打开HUD或其他操作
         *
         * 攻击交互比较特殊，不一定鼠标点到就能攻击到
         */
        
        private void InstructExecute()
        {
            Ray ray = Camera.main.ScreenPointToRay(_inputCtrlSystem.CursorPosInput);
            RaycastHit hitInfo;
            OperateKey ok = new OperateKey();
            
            if (_inputCtrlSystem.Execute0Input != 0f)
                ok = OperateKey.L;
            if (_inputCtrlSystem.Execute1Input != 0f)
                ok = OperateKey.R;
            
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (_inventory.GetItem() == null)
                {
                    
                    return;
                }
                
                if (hitInfo.transform.root.CompareTag("Entity"))
                {
                    hitInfo.transform.root.GetComponent<Entity>().Operate(ok, this);
                    if (hitInfo.transform.root.GetComponent<Entity>().CanOperate(ok, _inventory.GetItem()) 
                        && hitInfo.transform.root != transform)
                    {
                        this.GetModel<IPlayerModal>().PlayerHitInfo.Value = hitInfo.transform.root;
                        if (_inventory.GetItem().ItemProperties.ItemType == Item.ItemType.Weapon && ok == _inventory.GetItem().GetOperateKey())
                        {
                            _stateMachine.SwitchOnAction();
                        }
                    }
                }
                
                if(_inventory.GetItem().ItemProperties.ItemType == Item.ItemType.Food && ok == _inventory.GetItem().GetOperateKey())
                {
                    _stateMachine.SwitchOnAction();
                    _stateMachine.WhenCompleteProgress(()=>
                    {
                        _inventory.ReduceItemCount();
                    });

                }
            }
        }

        
        public GameObject GetHandHeldRBone() { return handHeldR; }
        public GameObject GetHandHeldLBone() { return handHeldL; }

        protected override void OnDie()
        {
            
        }
    }
}